package com.xxx.controller;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xxx.config.Request;
import com.xxx.pojo.Department;
import com.xxx.pojo.User;
import com.xxx.pojo.deleted.UserDeleted;
import com.xxx.service.UserDeletedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author George
 * @since 2023-07-04
 */
@Api(tags = "用户回收")
@CrossOrigin
@RestController
@RequestMapping("/userDeleted")
public class UserDeletedController {

    @Resource
    private UserDeletedService userDeletedService;

    /**
     * 查询所有已被禁用的用户
     *
     * @return 返回操作结果
     */
    @ApiOperation(value = "根据条件被禁用查询用户列表", notes = "根据分页查询参数查询用户列表")
    @PostMapping("/disabledUsers")
    public Request disabledUsers(
            @RequestParam("searchValue") String searchValue,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        Page<UserDeleted> mapPage = new Page<>(pageNum, pageSize, true);

        // 去除查询条件中的空格
        String finalSearchValue = StringUtils.isNotBlank(searchValue) ? searchValue.trim() : null;

        MPJLambdaWrapper<UserDeleted> lambdaWrapper = new MPJLambdaWrapper<UserDeleted>()
                .select(Department::getDepartmentName)
                .select(UserDeleted::getName,UserDeleted::getDeleted,UserDeleted::getUserName,
                        UserDeleted::getPhone,UserDeleted::getPerms,UserDeleted::getWorkNumber)
                .leftJoin(Department.class, Department::getDepartmentId, User::getDepartmentId);


        if (StringUtils.isNotBlank(finalSearchValue)) {
            lambdaWrapper.and(wrapper -> wrapper
                    .like(User::getPhone, finalSearchValue)
                    .or()
                    .like(User::getName, finalSearchValue)
                    .or()
                    .like(User::getUserName, finalSearchValue)
                    .or()
                    .like(Department::getDepartmentName, finalSearchValue));
        }

        userDeletedService.selectJoinListPage(mapPage,UserDeleted.class, lambdaWrapper);

        // 使用PageInfo对象包装查询结果
        List<UserDeleted> records = mapPage.getRecords();
        // 获取总记录数
        long total = mapPage.getTotal();
        return records.size() > 0 ? Request.suc(total,records) : Request.error("查询用户失败");
    }



    /**
     * 批量恢复用户权限
     *
     * @param workNumberList 员工工号列表
     * @return 返回操作结果
     */
    @ApiOperation(value = "批量恢复用户权限", notes = "根据工号批量恢复用户权限")
    @DeleteMapping("/restorePermissionsBatch")
    public Request restorePermissionsBatch(@RequestParam("workNumber") List<Integer> workNumberList) {
        boolean restored = userDeletedService.removeByIds(workNumberList);
        return restored ? Request.suc() : Request.error("根据工号批量恢复用户权限失败");
    }


}

